package com.digitaldreamers.ddmonsters.scene;

import java.io.IOException;
import java.net.UnknownHostException;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;

import com.digitaldreamers.ddmonsters.Constants;
import com.digitaldreamers.ddmonsters.network.packet.sending.SLoginByUiidRequest;
import com.digitaldreamers.framework.network.NetworkUtilities;
import com.digitaldreamers.framework.scene.SceneManager;

public class SplashScene extends BaseNetworkScene {
	private Sprite mLogo;
	
	@Override
	public Scene onLoadingSceneLoadAndShown() {
		return null;
	}

	@Override
	public void onLoadingSceneUnloadAndHidden() {
		
	}

	@Override
	public void onLoadScene() {
		mResourceManager.loadSplashSceneResources();
		mLogo = new Sprite(0, 0, mResourceManager.mLogoTextureRegion, mVBOM);
		mLogo.setPosition(mCameraMiddleX, mCameraMiddleY);
		attachChild(mLogo);
	}

	@Override
	public void onShowScene() {
		super.onShowScene();
		login();
	}

	@Override
	public void onHideScene() {
		
	}

	@Override
	public void onUnloadScene() {
		super.onUnloadScene();
		mResourceManager.unloadSplashSceneResources();
	}

	@Override
	protected void processPacket(int packetID, byte[] data) {
		
	}
	
	private void login() {
		try {
			mConnectionHandler.connectToServer(Constants.SERVER_IP, Constants.SERVER_PORT);
			String macAddress = NetworkUtilities.retrieveMacAddress(mActivity);
			SLoginByUiidRequest request = new SLoginByUiidRequest(macAddress);
			mConnectionHandler.send(request);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
